/*
 * @Author: wjc
 * @Date: 2025-02-13 14:52:31
 * @LastEditors: wjc
 * @LastEditTime: 2025-10-28 10:35:06
 * @Description:
 */
import { useUserStore } from '#/stores/modules/user-store'

import md5 from './md5'

export function formatAction(value: string) {
  value = `admin-${value}`
  // console.log(value)

  if (value.includes('Controller')) {
    value = value.replace('Controller', '')
    value = `controller-${md5(value).slice(8, 24)}`
  } else {
    value = md5(value).slice(8, 24)
  }
  return value
}

/**
 *
 * @param {Array} value
 * @returns {boolean}
 */
export default function checkAction(value: string) {
  const userStore = useUserStore()
  // 特殊情况下 值为空，可设置返回 true（如开发时），直接不进行验证
  if (value === '' || value === undefined) {
    return false
  }

  if (value === 'whitelist') {
    return true
  }

  const actions = userStore.actions
  // console.log(actions)
  // console.log(value, formatAction(value), actions.includes(formatAction(value)))
  if (actions && actions[0] === 'is_root') {
    return true
  }

  return actions?.includes(formatAction(value))
}
